<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class Member extends Model{
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $auto = ['status','code','create_time','update_time', 'last_login_ip'];

    /**
     * 获取用户
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field='*'){
        $list = $this
            ->with(['mch'=>[
                'id','name'
            ]])
            ->where($where)
            ->field($field)
            ->paginate($pageSize);
        return $list;
    }

    public function mch(){
        return $this->hasOne('Mch','id','mcid');
    }



    public function setCreateTimeAttr(){
        return time();
    }

    protected function setPasswordAttr($value){
        return substr($value,10,15);
    }

    public function setLastLoginIpAttr(){
        return request()->ip();
    }

    public function setCodeAttr($value){
        $code = implode(',',(array)$value);
        return $code;
    }

    protected function getTypeAttr($value){
        $type = ['','用户注册','系统生成'];
        return $type[$value];
    }

    protected function getStatusAttr($value){
        $type = ['','正常','禁用'];
        return $type[$value];
    }

    public function getMidAttr($value){
        $integerIDs = array_map('intval', explode(',', $value));
        return $integerIDs;
    }

    public function getCodeAttr($value){
        $code = explode(',',$value);
        return $code;
    }
    public function getAddressAttr($value){
        return  $value?$value:'未知';
    }
}